/**
 * Created by lobin on 2017/7/24.
 */
import { Application } from 'egg';
import * as pageFindPlugin from 'mongoose-paginate';
export default (app: Application) => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const ObjectId = Schema.Types.ObjectId;
  const schema = new mongoose.Schema({
    ctime: {
      type: Date,
      default: Date.now,
      required: true,
    },
    wash: {
      type: String,
      default: 0, // 0关闭 1 开启
      required: true,
    },
    name: {
      type: String,
      required: true,
      unique: true,
    },
    phone: {
      type: String,
      min: 7,
      max: 22,
      required: true,
      unique: true,
    },
    area: {
      type: Array,
      required: true,
    },
    com_area: {
      type: String,
      required: true,
    },
    address: {
      type: String,
      required: true,
    },
    sms_address: {
      type: String,
      required: true,
    },
    location: { type: [ Number ], default: [ 104.063401, 30.568744 ] },
    site_org: {
      type: ObjectId,
      required: true,
    },
    type: { type: Number, default: 1 }, // 0加盟,1为直营,2为承包
    stype: { type: Number, default: 0 }, // 编号规则 0顺序模式 1固定货架模式
    tc: { type: String, default: '21:00' }, // 下班时间
    to: { type: String, default: '9:00' }, // 上班时间
    qrsceneid: { type: Number }, // 微信参数二维码id
    qrticket: { type: String }, // 微信参数二维码ticket
    price: {
      sms: {
        type: Number,
        default: 5,
        get: (val: number) => val / 100,
        set: (val: number) => val * 100,
      },
      weixin: {
        type: Number,
        default: 3,
        get: (val: number) => val / 100,
        set: (val: number) => val * 100,
      },
    }, // 信息费价格
    acount: {
      type: Number,
      default: 0,
      get: (val: number) => val / 100,
      set: (val: number) => val * 100,
    },
    hasshelf: { type: Boolean, default: false },
    images: {
      type: Array,
      required: true,
    },
    // // 租房合同
    // rent_documnet: {
    // 	type: String,
    // },
    // // 承包合同
    // contract_document: {
    // 	type: String,
    // },
    status: {
      type: String,
      default: 0, // 默认为0,1为暂停使用,3为删除
    },
    logs: {
      type: Array,
    },
    remarks: {
      type: String,
    },
    eorderAddress: { type: String },
    cainiaoToken: { type: String },
    connect_status: {
      zhongtong: { type: Boolean, default: false },
      yunda: { type: Boolean, default: false },
      yuantong: { type: Boolean, default: false },
      baishi: { type: Boolean, default: false },
      shengtong: { type: Boolean, default: false },
    },
  }, {
    toJSON: { getters: true },
    timestamps: {
      updatedAt: true,
    },
    });

  // tslint:disable-next-line:ter-prefer-arrow-callback
  schema.virtual('photo').get(() => {
    return 'https://img.youbanghelper.com/img/bde716f4-a9c1-4ef6-9e14-a5586ad1d938.jpg';
  });
  schema.plugin(pageFindPlugin, {});
  schema.index({
    ctime: -1,
    name: 1,
    site_org: 1,
    type: 1,
    site: 1,
    status: 1,
  });
  return mongoose.model('Site', schema, 'sites');
};
